package org.absolutezero.client.view;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.absolutezero.client.style.Heading2Label;
import org.absolutezero.client.style.HeadingLabel;
import org.absolutezero.client.util.CellFactory;
import org.absolutezero.client.util.FormFactory;
import org.absolutezero.client.util.Session;
import org.absolutezero.shared.RecipeProxy;
import org.absolutezero.shared.RecipeRequest;
import org.absolutezero.shared.RefrigeratorRequestFactory;
import org.absolutezero.shared.SearchResultProxy;
import org.absolutezero.shared.ShelfProxy;
import org.absolutezero.shared.UserProxy;
import org.absolutezero.shared.UserRequest;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;

public class RecipesView extends Composite {

	private static RecipesViewUiBinder uiBinder = GWT
			.create(RecipesViewUiBinder.class);

	private final static HeadingLabel headingMatch = new HeadingLabel("Match recipe");
	private final static HeadingLabel headingMy = new HeadingLabel("My recipes");
	private final static HeadingLabel headingBrowse = new HeadingLabel("Browse recipes");
	private final static HeadingLabel headingAdd = new HeadingLabel("Add recipe");
	
	interface RecipesViewUiBinder extends UiBinder<Widget, RecipesView> {
	}

	@UiField
	HTMLPanel content;
	
	private final EventBus ebus;
	private final RefrigeratorRequestFactory requestFactory;
	
	@Inject
	public RecipesView(EventBus ebus,
			RefrigeratorRequestFactory requestFactory) {
		
		initWidget(uiBinder.createAndBindUi(this));
		this.ebus = ebus;
		this.requestFactory = requestFactory;
	}		
	
	public void setMatchRecipe(){
		content.clear();
		
		content.add(headingMatch);
		
		final VerticalPanel mainPanel = new VerticalPanel();
		final VerticalPanel resultsPanel = new VerticalPanel();
		
		UserProxy user = (UserProxy)Session.get("logged-user");
		UserRequest request = requestFactory.userRequest();
		user = request.edit(user);
		Request<List<ShelfProxy>> getUserRequest = request.fetchShelves().using(user);
		
		getUserRequest.fire(new Receiver<List<ShelfProxy>>(){

			@Override
			public void onSuccess(List<ShelfProxy> response) {
				
				CellTable<ShelfProxy> ct = CellFactory.getMatchShelves(response);
				Button btnSearch = new Button("Search");
				final MultiSelectionModel<ShelfProxy> sm = (MultiSelectionModel<ShelfProxy>)ct.getSelectionModel();
				btnSearch.addClickHandler(new ClickHandler(){

					@Override
					public void onClick(ClickEvent event) {
						
						RecipeRequest request2 = requestFactory.recipeRequest();
						List<ShelfProxy> lst = new ArrayList<ShelfProxy>();
						for(ShelfProxy s : sm.getSelectedSet()){
							lst.add(request2.edit(s));
						}
						
						Request<List<SearchResultProxy>> fetchResults = request2.findMatchingRecipes(lst).with("recipe.author");
						fetchResults.fire(new Receiver<List<SearchResultProxy>>(){

							@Override
							public void onSuccess(List<SearchResultProxy> response) {
								resultsPanel.clear();
								
								CellTable<SearchResultProxy> ct = CellFactory.getSearchResultsTableView(response);
								resultsPanel.add(ct);
							}
							
						});
					}
					
				});
				mainPanel.add(ct);
				mainPanel.add(btnSearch);
			}
			
		});
		content.add(new Heading2Label("Your Shelves"));
		content.add(mainPanel);
		content.add(new Heading2Label("Results"));
		content.add(resultsPanel);
	}
	
	public void setMyRecipes(){
		content.clear();

		content.add(headingMy);
		
		VerticalPanel panel = new VerticalPanel();
		
		Button btnAddRecipe = new Button("Add Recipe");
		final VerticalPanel panel2 = new VerticalPanel();
		RecipeRequest request = requestFactory.recipeRequest();
		UserProxy user = (UserProxy)Session.get("logged-user");
		
		Request<List<RecipeProxy>> fetchReq = request.getUserRecipes(user.getId());
		fetchReq.fire(new Receiver<List<RecipeProxy>>(){

			@Override
			public void onSuccess(List<RecipeProxy> response) {
				
				CellTable<RecipeProxy> ct = CellFactory.getMyRecipesTableView(response);
				panel2.add(ct);
				
			}
			
		});
		
		btnAddRecipe.addClickHandler(new ClickHandler(){

			@Override public void onClick(ClickEvent event) { setEditRecipe(); }
			
		});
		
		panel.add(panel2);
		panel.add(btnAddRecipe);
		panel.setCellHorizontalAlignment(btnAddRecipe, VerticalPanel.ALIGN_RIGHT);
		content.add(panel);
		
	}
	
	public void setBrowseRecipes(){
		content.clear();

		content.add(headingMy);
		
		VerticalPanel panel = new VerticalPanel();
		
		final VerticalPanel panel2 = new VerticalPanel();
		RecipeRequest request = requestFactory.recipeRequest();
		
		Request<List<RecipeProxy>> fetchReq = request.getAllRecipes().with("author");
		fetchReq.fire(new Receiver<List<RecipeProxy>>(){

			@Override
			public void onSuccess(List<RecipeProxy> response) {
				
				CellTable<RecipeProxy> ct = CellFactory.getBrowseRecipesTableView(response);
				panel2.add(ct);
				
			}
			
		});
		
		panel.add(panel2);
		content.add(panel);
	}
	
	public void setEditRecipe(){
		content.clear();
		
		content.add(headingAdd);
		
		content.add(FormFactory.getEditRecipe(null, requestFactory));
	}
	
	public void setEditRecipe(RecipeProxy r){
		content.clear();
		
		content.add(headingAdd);
		
		content.add(FormFactory.getEditRecipe(r, requestFactory));
	}
	
	public void setViewRecipe(RecipeProxy r){
		content.clear();
		
		content.add(new HeadingLabel(r.getName()));
		content.add(FormFactory.getViewRecipe(r, requestFactory));		
		
	}

}
;